﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUSService;
using DTOService;

namespace QuanLyBangDia
{
    public partial class frmGhiNhanMatSanPham : Form
    {
        //Thien code
        MyService myservice = new MyService();
        public frmGhiNhanMatSanPham()
        {
            InitializeComponent();
        }

        private void frmGhiNhanMatSanPham_Load(object sender, EventArgs e)
        {
            try
            {
                BangDia[] arr = myservice.SelectBangDia();
                AutoCompleteStringCollection arrCollection1 = new AutoCompleteStringCollection();
                foreach (BangDia bd in arr)
                {
                    arrCollection1.Add(bd.MaBD);
                }
                txtMaBD.AutoCompleteMode = AutoCompleteMode.Suggest;
                txtMaBD.AutoCompleteSource = AutoCompleteSource.CustomSource;
                txtMaBD.AutoCompleteCustomSource = arrCollection1;
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void txtMaBD_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtMaBD.Text) == false)
                {
                    BangDia temp = myservice.GetBangDia(txtMaBD.Text);

                    txtTenBD.Text = temp.TenBD;
                    txtMoTa.Text = temp.MoTa;
                    txtLoaiBD.Text = temp.LoaiBD.ToString();
                    txtLoaiSP.Text = temp.LoaiSanPham.ToString();
                    txtTinhTrang.Text = temp.TinhTrang.ToString();
                    txtSoLuong.Text = temp.SoLuong.ToString();
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void btnGhiNhanMat_Click(object sender, EventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtMaBD.Text) == false)
                {
                    int kq=myservice.GhiNhanTinhTrang(txtMaBD.Text,0);
                    if (kq == 1)
                    { MessageBox.Show("Ghi nhận thành công"); txtSoLuong.Text = "0"; }
                    else
                        MessageBox.Show("Ghi nhận không thành công");
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void btnBinhThuong_Click(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(txtSoLuong.Text) != 0)
                {
                    if (String.IsNullOrEmpty(txtMaBD.Text) == false)
                    {
                        int kq = myservice.GhiNhanTinhTrang(txtMaBD.Text, int.Parse(txtSoLuong.Text));
                        if (kq == 1)
                        { MessageBox.Show("Ghi nhận thành công"); }
                        else
                            MessageBox.Show("Ghi nhận không thành công");
                    }
                }
                else
                { MessageBox.Show("Nhập số lượng sản phẩm!"); txtSoLuong.Focus(); }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void txtSoLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

    }
}
